package com.cuif.ioc.lifecycle;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.cuif.pojo.User;

/**
 * LifeCycleService spring 生命周期演示
 * 1.new
 * 2.set DI
 * 3.BeanNameAware:  bean名关注
 * 4.BeanFactoryAware:  bean工厂关注
 * 5.BeanPostProcessor.beforeInitialization();
 * 5'.调用InitializingBean.afterPropertySet()方法.
 * 6.init-method
 * 7.BeanPostProcessor.afterInitialization();
 * 
 */
public class LifeCycleService implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean {
	private String greeting ;
	private String greeting2 ;
	
	/* byeService */
	private User user;
	
	public LifeCycleService(){}
	
	public LifeCycleService(User user){
		this.user = user ;
		System.out.println("Constructor: new GreetingService()");
	}
	public LifeCycleService(String str1,String str2){
		System.out.println("1111");
	}
	public LifeCycleService(String str1,Integer str2){
		System.out.println("2222");
	}
	public LifeCycleService(String str1,int str2){
		System.out.println("3333");
	}
	public LifeCycleService(int str1,String str2){
		System.out.println("4444");
	}
	
	/**
	 * beannameAware,注入bean的id
	 */
	public void setBeanName(String name) {
		System.out.println("BeanNameAware:setBeanName() : " + name);
	}
	
	/**
	 * bean工厂关注
	 */
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		System.out.println("BeanFactoryAware:setBeanFactory() : " + beanFactory);
	}
	
	/**
	 * 上下文关注
	 */
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		System.out.println("ApplicationContextAware:setApplicationContext() : " + applicationContext);
	}

	public User getUser() {
		return user;
	}
	
	public void setUser(User user) {
		this.user = user;
		System.out.println("DI:setUser() : " + user);
	}
	
	public String getGreeting() {
		return greeting;
	}

	public void setGreeting(String greeting) {
		this.greeting = greeting;
		System.out.println("DI:setGreeting() : " + greeting);
	}
	
	public void sayGreeting(){
		System.out.println(" sayGreeting : userName = " + user.getName() + ", userAge = " + user.getAge());
	}
	public void sayGreeting2(){
		System.out.println(greeting2);
	}

	public String getGreeting2() {
		return greeting2;
	}

	public void setGreeting2(String greeting2) {
		this.greeting2 = greeting2;
		System.out.println("DI:setGreeting2() : " + greeting2);
	}
	
	/**
	 * 定制初始化方法
	 */
	public void ini(){
		System.out.println("ini");
	}
	
	/**
	 * 自定义初始化方法-接口
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("afterPropertiesSet");
	}
	
	/**
	 * 自定义初始化方法- 注解
	 */
	@PostConstruct
	public void ini2(){
		System.out.println("ini2");
	}
	
	
	/**
	 * 定制销毁方法
	 */
	public void release(){
		System.out.println("release");
	}
	
	/**
	 * 定制销毁方法 - 注解
	 */
	@PreDestroy
	public void release2(){
		System.out.println("release2");
	}

}
